package com.bojiu.lawsearch.lawsearch.wxzf;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class SubmitOrderHandler {

    @Autowired
    WXZFHttpHandler wxzfHttpHandler;

    /**
     * 通用下单且再次签名接口 必须含有openid
     * 若为扫码模式一时product_id不能为空
     *
     * @param openid
     * @param body        商品描述
     * @param trade_type  交易类型
     * @param attach      附加数据
     * @param orderNum    订单号
     * @param fee         金额
     * @param request     请求
     * @param device_info 设备号
     * @return
     * @throws Exception
     */
    public Map<String, String> handleSubmitOrder(String openid, String body, String trade_type,
                                                 String attach, String orderNum, int fee, HttpServletRequest request, String device_info, String product_id)
            throws Exception {
        Map<String, String> resultMap = new HashMap<>();
        if (StringUtils.isEmpty(openid)) {
            log.error("openid不能为空");
            resultMap.put("false", "false");
            return resultMap;
        }
        if ("NATIVE ".equals(trade_type)) {
            if (StringUtils.isEmpty(product_id)) {
                log.error("product_id不能为空");
                resultMap.put("false", "false");
                return resultMap;
            }
        }
        Map<String, String> dataMap = WXZFUtil.createDataMap(WXZFUtil.generateNonceStr(), null, openid, body,
                orderNum, fee, WXZFUtil.getIpAddress(request), WXZFConstance.INFORMURL, trade_type, attach, device_info,
                "no_credit", product_id);
        Map<String, String> responsetMap = wxzfHttpHandler.doPost(WXZFUtil.mapToXml(dataMap),
                WXZFConstance.SUBMITORDER);
        if (!responsetMap.containsKey("prepay_id") || StringUtils.isEmpty(responsetMap.get("prepay_id"))) {
            log.error("提交订单失败");
            resultMap.put("false", "false");
            return resultMap;
        }
        String prepay_id = responsetMap.get("prepay_id");

        log.debug("提交订单成功，开始再次签名");

        String time = String.valueOf(System.currentTimeMillis() / 1000);
        String newNoncestr = WXZFUtil.generateNonceStr();
        resultMap = WXZFUtil.createZFData(time, newNoncestr, prepay_id);
        return resultMap;
    }

    /**
     * 扫码支付  单次统一提交（不再次签名） product_id不能为空 openid可以为空
     *
     * @param openid
     * @param body
     * @param trade_type
     * @param attach
     * @param orderNum
     * @param fee
     * @param request
     * @param device_info
     * @return
     * @throws Exception
     */
    public Map<String, String> onlySubmitOrder(String openid, String body, String trade_type,
                                               String attach, String orderNum, int fee, HttpServletRequest request, String device_info, String product_id)
            throws Exception {
        if (StringUtils.isEmpty(product_id)) {
            log.error("product_id不能为空");
            throw new Exception();
        }
        Map<String, String> dataMap = WXZFUtil.createDataMap(WXZFUtil.generateNonceStr(), null, openid, body,
                orderNum, fee, WXZFUtil.getIpAddress(request), WXZFConstance.INFORMURL, trade_type, attach, device_info,
                "no_credit", product_id);
        Map<String, String> responsetMap = wxzfHttpHandler.doPost(WXZFUtil.mapToXml(dataMap),
                WXZFConstance.SUBMITORDER);
        if (!responsetMap.containsKey("prepay_id") || StringUtils.isEmpty(responsetMap.get("prepay_id"))) {
            log.error("提交订单失败");
            responsetMap.put("false", "false");
            return responsetMap;
        }
        return responsetMap;
    }


}
